package com.szewec.ps.util.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.szewec.ps.R;
import com.szewec.ps.util.ConvertDpAndPx;
import butterknife.BindView;
import butterknife.ButterKnife;

public class CommonDialog extends Dialog {

    @BindView(R.id.dialog_title)
    TextView dialogTitle;
    @BindView(R.id.dialog_content)
    TextView dialogContent;
    @BindView(R.id.dialog_content2)
    TextView dialogContent2;
    @BindView(R.id.dialog_cancel)
    Button dialogCancel;
    @BindView(R.id.dialog_ensure)
    Button dialogEnsure;
    @BindView(R.id.ll_button)
    LinearLayout llButton;
    @BindView(R.id.view)
    View view;

    private String title;
    private String content;
    private String content2;
    private String cancel;
    private String ensure;
    private View.OnClickListener onEnsureClickListener;
    private View.OnClickListener onCancelClickListener;
    private final Context mContext;

    public CommonDialog(@NonNull Context context) {
        super(context, R.style.dialog_Common);
        mContext = context;
        setContentView(R.layout.dialog_common);
        ButterKnife.bind(this);
    }

    /*
     * 设置标题 默认没有标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
    }
    /**
     * 设置内容 默认为空
     *
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
    }
    /**
     * 设置内容 默认为空
     *
     * @param content
     */
    public void setContent2(String content) {
        this.content2 = content;
    }
    /**
     * 设置确定按钮内容 默认为确定
     *
     * @param ensure
     */
    public void setEnsure(String ensure) {
        this.ensure = ensure;
    }
    /**
     * 设置取消按钮内容 默认为取消
     *
     * @param cancel
     */
    public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    /**
     * 确定按钮事件监听 默认是dismiss对话框
     *
     * @param onEnsureClickListener
     */
    public void setOnEnsureClickListener(View.OnClickListener onEnsureClickListener) {
        this.onEnsureClickListener = onEnsureClickListener;
    }
    /**
     * 取消按钮事件监听 默认是dismiss对话框
     *
     * @param onCabcelClickListener
     */
    public void setOnCancelClickListener(View.OnClickListener onCabcelClickListener) {
        this.onCancelClickListener = onCabcelClickListener;
    }

    /**
     * 重写show方法
     */
    @Override
    public void show() {
        if (TextUtils.isEmpty(title)) {
            //默认没有标题
            dialogTitle.setVisibility(View.GONE);
        } else {
            //默认不设置内容，则内容太为空
            dialogTitle.setVisibility(View.VISIBLE);
            setTextViewTxt(dialogTitle, title);
        }
        if (TextUtils.isEmpty(cancel)) {
            //默认取消按钮文字为"取消"
            cancel = mContext.getString(R.string.cancel);
        }
        if (TextUtils.isEmpty(ensure)) {
            //默认确认按钮文字为"确认"
            ensure = mContext.getString(R.string.ensure);
        }
        //设置文字信息
        setTextViewTxt(dialogContent, content);
        setTextViewTxt(dialogContent2, content2);
        setTextViewTxt(dialogCancel, cancel);
        setTextViewTxt(dialogEnsure, ensure);
        //设置点击事件
        setButtonOnClickListener(dialogCancel, onCancelClickListener);
        setButtonOnClickListener(dialogEnsure, onEnsureClickListener);
        super.show();
    }

    private void setButtonOnClickListener(Button dialogCancel, View.OnClickListener onCancelClickListener) {
        dialogCancel.setOnClickListener(onCancelClickListener);
    }


    private void setTextViewTxt(TextView dialogTitle, String title) {
        dialogTitle.setText(title);
    }

    //设置成只有ok模式的样式
    public void setModelOk(){
        dialogCancel.setVisibility(View.GONE);
        view.setVisibility(View.GONE);
        dialogEnsure.setBackgroundResource(R.drawable.btn_click_bg_ok_2);
    }
    //设置文本内容左右间距
    public void setContentMargin(){
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(ConvertDpAndPx.Dp2Px(mContext,25), ConvertDpAndPx.Dp2Px(mContext,5), ConvertDpAndPx.Dp2Px(mContext,25), ConvertDpAndPx.Dp2Px(mContext,0));
        dialogContent.setLayoutParams(lp);
        dialogContent2.setVisibility(View.VISIBLE);
    }



}
